package com.hockeo.client.ui.views;

import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.dom.client.Style;
import com.google.gwt.user.client.ui.Widget;
import com.hockeo.client.res.AppConstants;
import com.hockeo.shared.VHUtil;
import com.hockeo.shared.domain.persistent.Manager;

/**
 * Application status view that displays who is the currently connected user and provides
 * access to other information (e.g. parameters, login/logout etc.).
 *
 * @version $Id$
 * @author jjanke
 */
public class StatusView extends ToolBar implements IView
{
  private final Button            d_btnLogInOrOut;
  private final LabelToolItem     d_lblConnectedUser;

  public StatusView()
  {
    add( new FillToolItem() );

    d_lblConnectedUser = new LabelToolItem();
    d_lblConnectedUser.setStyleAttribute( "font-weight", Style.FontWeight.BOLD.getCssName() );
    add( d_lblConnectedUser );

    d_btnLogInOrOut = new Button();
    d_btnLogInOrOut.setVisible( false );
    d_btnLogInOrOut.setStyleAttribute( "margin-left", "4px" );
    add( d_btnLogInOrOut );
  }

  @Override
  public Widget getViewWidget()
  {
    return this;
  }

  /**
   * Displays information about the currently connected user or clears it if nobody is
   * connected. If a user is passed in, a logout button is also added to the tool bar. If
   * <code>null</code> is passed, a login button is displayed.
   *
   * @param manager the manager that is currently connected or <code>null</code> to
   *          indicate that nobody is connected
   */
  public void updateUserInfo( Manager manager )
  {
    AppConstants c = VHUtil.constants();

    if ( manager != null )
    {
      d_lblConnectedUser.setLabel( manager.getEmail() );
      d_btnLogInOrOut.setText( c.logout() );
      d_btnLogInOrOut.setIcon( VHUtil.ICON_LOGOUT );
    }
    else
    {
      d_lblConnectedUser.setLabel( c.notLoggedIn() );
      d_btnLogInOrOut.setText( c.login() );
      d_btnLogInOrOut.setIcon( VHUtil.ICON_LOGIN );
    }

    if ( !d_btnLogInOrOut.isVisible() )
      d_btnLogInOrOut.setVisible( true );
  }

  public Button getLogInOutButton()
  {
    return d_btnLogInOrOut;
  }
}
